package com.kujie.access.service.impl;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kujie.access.comparator.NaturalOrderComparator;
import com.kujie.access.domain.po.RegionManagement;
import com.kujie.access.domain.vo.RegionManagementVO;
import com.kujie.access.mapper.RegionManagementMapper;
import com.kujie.access.service.IRegionService;
import com.kujie.common.core.utils.StringUtils;
import com.kujie.common.core.utils.TreeBuildUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RegionServiceImpl implements IRegionService {
    private final RegionManagementMapper regionManagementMapper;

    @Override
    public List<Tree<String>> list(RegionManagement regionManagement) {
        List<Tree<String>> treeList = new ArrayList<>();

        // 构建查询条件
        LambdaQueryWrapper<RegionManagement> regionLqw = new LambdaQueryWrapper<>();
        constructPropertyQuery(regionManagement, regionLqw);
        List<RegionManagement> regionManagements = regionManagementMapper.selectList(regionLqw).stream().sorted(Comparator.comparing(RegionManagement::getIndexCode, new NaturalOrderComparator())).toList();

        if (CollectionUtils.isEmpty(regionManagements)) {
            return treeList;
        }

        treeList = TreeBuildUtils.buildRegion(regionManagements, (
                data,
                tree) ->
                tree.setId(data.getIndexCode())
                    .setParentId(data.getParentIndexCode())
                    .setName(data.getName())
                    .putExtra("createTime", data.getCreateTime()),
            "parentIndexCode");

        return treeList;
    }

    @Override
    public int delete(String indexCode) {
        if (StringUtils.isNotEmpty(indexCode)) {
            LambdaQueryWrapper<RegionManagement> regionManagementLambdaQueryWrapper = new LambdaQueryWrapper<RegionManagement>().eq(RegionManagement::getIndexCode, indexCode);
            return regionManagementMapper.delete(regionManagementLambdaQueryWrapper);
        }

        return 0;
    }

    @Override
    public boolean insertOrUpdate(RegionManagement regionManagement) {
        return regionManagementMapper.insertOrUpdate(regionManagement);
    }

    @Override
    public RegionManagementVO detail(String indexCode) {
        RegionManagement regionManagement = regionManagementMapper.selectOne(new LambdaQueryWrapper<RegionManagement>().eq(StringUtils.isNotEmpty(indexCode), RegionManagement::getIndexCode, indexCode));
        RegionManagementVO regionManagementVO = new RegionManagementVO();
        BeanUtils.copyProperties(regionManagement, regionManagementVO);
        return regionManagementVO;
    }

    private void constructPropertyQuery(RegionManagement regionManagement, LambdaQueryWrapper<RegionManagement> regionLqw) {
        regionLqw.like(StringUtils.isNotEmpty(regionManagement.getAffiliatedPark()), RegionManagement::getAffiliatedPark, regionManagement.getAffiliatedPark());
        regionLqw.like(StringUtils.isNotEmpty(regionManagement.getParentName()), RegionManagement::getParentName, regionManagement.getParentName());
        regionLqw.like(StringUtils.isNotEmpty(regionManagement.getName()), RegionManagement::getName, regionManagement.getName());
    }
}
